<?php

namespace App\Services\CancelProductOrder;

use App\Models\Order;
use Illuminate\Support\Facades\DB;

class CancelProductOrder
{
	protected $order;
	
	public function __construct(Order $order)
	{
		$this->order = $order;
	}
	
	protected function createRefund()
	{
		$order = $this->order;
		
		return $order->user->orderRefunds()->create([
			'order_id' => $order->id,
			'type' => 'return_money_only',
			'reason' => 'other_problem',
			'refund_money' => $order->total_amount,
			'parcel_status' => 'unreceived',
			'refund_status' => 'confirmed',
			'is_order_cancel' => true,
		]);
	}
	
	public function cancel()
	{
		DB::transaction(function () {
			$refund = $this->createRefund();
			cashier($this->order->paid_method)->refundOrder($refund);
		});
	}
}
